height=[[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]

def dfs(height,can_reach,r,c):
    if can_reach[r][c]:
        return None
    direction = [-1, 0, 1, 0, -1]
    can_reach[r][c]=True
    for i in range(4):
        x,y=r+direction[i],c+direction[i+1]
        if x>=0 and x<len(height) and y>=0 and y<len(height[0]) and height[r][c]<=height[x][y]:
            dfs(height,can_reach,x,y)
def pacificAtlantic(height):
    res=[]
    row_len,col_len=len(height),len(height[0])
    can_reach_p=[[False for i in range(col_len)] for j in range(row_len)]
    can_reach_a=[[False for i in range(col_len)] for j in range(row_len)]
    for i in range(row_len):
        dfs(height,can_reach_p,i,0)
        dfs(height,can_reach_a,i,col_len-1)
    for i in range(col_len):
        dfs(height,can_reach_p,0,i)
        dfs(height,can_reach_a,row_len-1,i)
    for i in range(row_len):
        for j in range(col_len):
            if can_reach_a[i][j] and can_reach_p[i][j]:
                res.append([i,j])
    return res

print(pacificAtlantic(height))